package interfacePackageStructures;

import java.util.ArrayList;
import java.util.Iterator;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.table.AbstractTableModel;

import mapPackage.Route;
import xmlPackage.Carte;

public class MyTableVillesIntermediaires extends AbstractTableModel { 
	
	
	Icon iconSupprimer = new ImageIcon(getClass().getResource("/images/supprimer.gif"));

	private String[] columnNames = {"Nom",
                                    "A suivre"};
	
	ArrayList<VilleIntermediaire> data = new ArrayList<VilleIntermediaire>();

	public ArrayList<VilleIntermediaire> getData() {
		return data;
	}
	
    public int getColumnCount() {
        return columnNames.length;
    }

    public int getRowCount() {
        return data.size();
    }

    public String getColumnName(int col) {
        return columnNames[col];
    }

    public Object getValueAt(int row, int col) {
    	if (col == 0) return data.get(row).getNom();
    	if (col == 1) return data.get(row).isAsuivre();
    	
    	else return "";
    }

    /*
     * JTable uses this method to determine the default renderer/
     * editor for each cell.  If we didn't implement this method,
     * then the last column would contain text ("true"/"false"),
     * rather than a check box.
     */
    public Class getColumnClass(int c) {
        return getValueAt(0, c).getClass();
    }

    /*
     * Don't need to implement this method unless your table's
     * editable.
     */
    public boolean isCellEditable(int row, int col) {
        //Note that the data/cell address is constant,
        //no matter where the cell appears onscreen.
        if (col == 0) {
            return false;
        } else {
            return true;
        }
    }

    /*
     * Don't need to implement this method unless your table's
     * data can change.
     */
    public void setValueAt(Object value, int row, int col) {           

    	//si la ligne existe pas on la cree sinon modif
    	if ((data.size() <= row) || (data.size() == 0 )){
    			data.add(new VilleIntermediaire((String)value));
    	}
    	else {
    		VilleIntermediaire w_villeInter = data.get(row);
    		if (col == 0) w_villeInter.setNom((String)value);
    		else if (col == 1) {
    			w_villeInter.setAsuivre((Boolean)value);
    		}
        fireTableCellUpdated(row, col);
    	}
    }

    private void printDebugData() {
        int numRows = getRowCount();
      
        for (int i=0; i < numRows; i++) {
            System.out.print("    row " + i + ":");
                System.out.print("  " + data.get(i).getNom());
                System.out.print("  " + data.get(i).isAsuivre());
        System.out.println("--------------------------");
        }
    }
    
    public boolean IsVillePresente(String nom) {
    	
    	Iterator<VilleIntermediaire> it = data.iterator();
    	while (it.hasNext()) {
			VilleIntermediaire w_villeCourante = (VilleIntermediaire) it.next();
			if (w_villeCourante.getNom().equals(nom)) return true;
		}
    	
    	return false;
    }
}